package org.javaboy.weixin_login.config;

import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;

import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * OAuth2AuthorizationRequest 实际上就是授权请求，这个请求是用来获取 code 的
 */
public class WeiXinAuthorizationRequestConsumer implements Consumer<OAuth2AuthorizationRequest.Builder> {
    @Override
    public void accept(OAuth2AuthorizationRequest.Builder builder) {
        OAuth2AuthorizationRequest request = builder.build();
        if ("weixin".equals(request.getAttribute(OAuth2ParameterNames.REGISTRATION_ID))) {
            builder.parameters(params -> {
                params.clear();
                params.put("appid", request.getClientId());
                params.put("redirect_uri", request.getRedirectUri());
                params.put("response_type", request.getResponseType().getValue());
                params.put("scope", request.getScopes().stream().collect(Collectors.joining(",")));
                params.put("state", request.getState());
                params.put("forcePopup", true);
            });
        }
    }
}
